<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="饰品名称" prop="gunsName">
        <el-input
          v-model="queryParams.gunsName"
          placeholder="请输入饰品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="isType">
        <el-select
          v-model="queryParams.isType"
          placeholder="状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in isTypeOptions"
            :key="dict.val"
            :label="dict.label"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属vip等级" prop="gunsLevel">
        <el-select
          v-model="queryParams.gunsLevel"
          placeholder="实名信息"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in vipOptions"
            :key="dict.id"
            :label="dict.vipName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:guns:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:guns:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:guns:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:guns:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gunsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="饰品名称" align="center" prop="gunsName" />
      <el-table-column label="封面图片" align="center" prop="imgUrl" >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.imgUrl"
            :preview-src-list="[scope.row.imgUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="饰品价格" align="center" prop="amount" />
      <el-table-column label="当前市场指导价" align="center" prop="zamount" />
      <el-table-column label="库存" align="center" prop="gunsNum" />
      <el-table-column label="利润金额" align="center" prop="moneyAmount" />
      <el-table-column label="所属vip等级" align="center" prop="gunsLevel" :formatter="vipFormatter" />
      <el-table-column label="状态" align="center" prop="isType" >
        <template slot-scope="scope">
          {{scope.row.isType == 0? '下架' : '上架'}}
        </template>
      </el-table-column>
      <el-table-column label="购买后多长时间卖出(小时为单位)" align="center" prop="buyHour" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:guns:edit']"
          >修改</el-button>

          <el-button v-if="scope.row.isType == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="gunsUp(scope.row)"

          >上架</el-button>
          <el-button v-else
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="gunsDown(scope.row)"

          >下架</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改饰品商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="饰品名称" prop="gunsName">
          <el-input v-model="form.gunsName" placeholder="请输入饰品名称" />
        </el-form-item>

<!--        <el-form-item label="饰品参数" prop="gunsParam">
          <el-input v-model="form.gunsParam"  placeholder="请输入饰品参数" />
        </el-form-item>-->
        <el-form-item label="封面图片" prop="imgUrl">
          <el-upload
            class="avatar-uploader"
            :action="fileUrl"
            :show-file-list="false"
            :on-success="successFile"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
        </el-form-item>
        <el-form-item label="饰品价格" prop="amount">
          <el-input-number
            v-model="form.amount"
            :precision="2"
            :step="1"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="当前市场指导价" prop="zAmount">
          <el-input-number
            v-model="form.zamount"
            :precision="2"
            :step="1"
            :min="1"
          ></el-input-number>        </el-form-item>
        <el-form-item label="库存" prop="gunsNum">
          <el-input-number
            v-model="form.gunsNum"
            :precision="0"
            :step="1"
            :min="1"
          ></el-input-number>         </el-form-item>
        <el-form-item label="利润金额" prop="moneyAmount">
          <el-input-number
            v-model="form.moneyAmount"
            :precision="2"
            :step="1"
            :min="1"
          ></el-input-number>          </el-form-item>
        <el-form-item label="状态" prop="isType">
          <el-select
            v-model="form.isType"
            placeholder="状态"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in isTypeOptions"
              :key="dict.val"
              :label="dict.label"
              :value="dict.val"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属vip等级" prop="gunsLevel">
          <el-select
            v-model="form.gunsLevel"
            placeholder="实名信息"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in vipOptions"
              :key="dict.id"
              :label="dict.vipName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="购买后多长时间卖出(小时为单位)" prop="buyHour">
          <el-input-number
            v-model="form.buyHour"
            :precision="0"
            :step="1"
            :min="1"
          ></el-input-number>         </el-form-item>
        <el-form-item label="详情描述">
          <editor v-model="form.gunsContent" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGuns, getGuns, delGuns, addGuns, updateGuns, exportGuns } from "@/api/admin/guns";
import Editor from '@/components/Editor';
import { listVip_level} from "@/api/admin/vip_level";
import {fileUrl} from "@/constant/constant"

export default {
  name: "Guns",
  components: { Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      fileUrl:'',
      //0下架 1上架
      isTypeOptions:[
        {
          label:"下架",
          val:0
        },
        {
          label:"上架",
          val:1
        },
      ],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 饰品商品表格数据
      gunsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      vipOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gunsName: null,

        gunsContent: null,

        gunsParam: null,

        imgUrl: null,

        amount: null,

        zAmount: null,

        gunsNum: null,

        moneyAmount: null,

        gunsLevel: null,

        isType: null,

        buyHour: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.fileUrl = fileUrl;

    this.getList();
    listVip_level({pageNum: 1,pageSize: 1000000}).then(res =>{
      this.vipOptions = res.rows;
    })
  },
  methods: {
    /** 查询饰品商品列表 */
    getList() {
      this.loading = true;
      listGuns(this.queryParams).then(response => {
        this.gunsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 上架
    gunsUp(entity){

      getGuns(entity.id).then(res =>{
        var fEntity = res.data;
        fEntity.isType = 1;
        updateGuns(fEntity).then(res =>{
          this.msgSuccess("修改成功");
          this.getList();
        });
      });
    },
    // 下架
    gunsDown(entity){
      getGuns(entity.id).then(res =>{
        var fEntity = res.data;
        fEntity.isType = 0;
        updateGuns(fEntity).then(res =>{
          this.msgSuccess("修改成功");
          this.getList();
        });
      });
    },
    vipFormatter(row){
      var tempName = '';
      this.vipOptions.forEach(item =>{
        if (row.gunsLevel == item.id)
          tempName =  item.vipName;
      });
      return tempName;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    successFile(res, file) {
      this.form.imgUrl = URL.createObjectURL(file.raw);
      this.form.imgUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;


      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return  isLt2M;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        gunsName: null,

        gunsContent: null,

        gunsParam: null,

        imgUrl: null,

        amount: null,

        zAmount: null,

        gunsNum: null,

        moneyAmount: null,

        gunsLevel: null,

        isType: null,

        createTime: null,

        buyHour: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加饰品商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGuns(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改饰品商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGuns(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGuns(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除饰品商品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGuns(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有饰品商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGuns(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
